//clase para enviar datos al puerto serial
package pruebas;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.*;

public class EnviaDatosPuerto extends Thread {

  static Enumeration listaPuertos;
  static CommPortIdentifier idPuerto;
  
  String Sustrato1_1 = "A-0.333-0.294-0.686-0.725-0.8-1-1-";
  String Sustrato1_2 = "A-1.411-1.411-1.411-1.411-0.8-1-1-";
  String Sustrato1_3 = "A-1.078-1.078-1.078-1.078-0.8-1-1-";
  String Sustrato1_4 = "A-1.431-1.431-1.431-1.431-0.8-1-1-";
  String Sustrato1_5 = "A-1.392-1.392-1.392-1.392-0.8-1-1-";
  String Sustrato1_6 = "A-1.372-1.372-1.372-1.372-0.8-1-1-";
  String Sustrato1_7 = "A-1.411-1.411-1.411-1.411-0.8-1-1-";
  String Sustrato1_8 = "A-1.45-1.45-1.45-1.45-0.8-1-1-";
  String Sustrato1_9 = "A-1.431-1.431-1.431-1.431-0.8-1-1-";
  
  
  String Sustrato2_1 = "B-0.764-0.764-0.725-0.764-0.95-1-1-";
  String Sustrato2_2 = "B-0.803-0.784-0.745-0.803-0.95-0-1-";
  String Sustrato2_3 = "B-0.803-0.784-0.764-0.803-0.95-1-1-";
  String Sustrato2_4 = "B-0.764-0.764-0.725-0.784-0.95-0-1-";
  String Sustrato2_5 = "B-0.666-0.725-0.803-0.843-0.8-1-1-";
  String Sustrato2_6 = "B-0.764-0.764-0.725-0.764-0.95-1-1-";
  String Sustrato2_7 = "B-0.803-0.784-0.745-0.803-0.95-0-1-";
  String Sustrato2_8 = "B-0.803-0.784-0.764-0.803-0.95-1-1-";
  String Sustrato2_9 = "B-0.764-0.764-0.725-0.784-0.95-0-1-";
  String Sustrato2_10 = "B-0.666-0.725-0.803-0.843-0.8-1-1-";
  
  String paquete1[] = {Sustrato1_1,
//                       Sustrato1_2,
//                       Sustrato2_1, 
//                       Sustrato2_2,
//                       Sustrato3_1,
//                       Sustrato3_2,
//                       Sustrato3_3,
//                       Sustrato3_4
  };
                     
  static SerialPort puertoSerie;
  static OutputStream salida;

  public static void main(String[] args) {
    EnviaDatosPuerto obj = new EnviaDatosPuerto();
    obj.start();
  }

  @Override
  public void run() {
    listaPuertos = CommPortIdentifier.getPortIdentifiers();
    while (listaPuertos.hasMoreElements()) {
      idPuerto = (CommPortIdentifier) listaPuertos.nextElement();
      if (idPuerto.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        if (idPuerto.getName().equals("COM2")) {
          try {
            puertoSerie = (SerialPort) idPuerto.open("AplEscritura", 2000);
          } catch (PortInUseException e) {
          }
          try {
            salida = puertoSerie.getOutputStream();
          } catch (IOException e) {
          }
          try {
            puertoSerie.setSerialPortParams(9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
          } catch (UnsupportedCommOperationException e) {
          }
            try {
              int i=0;

              sleep(500);
             
              for (int y = 0; y < 1; y++) {
                for (int x = 0; x < 1; x++) {
                  salida.write(paquete1[x].getBytes());
                  System.out.println("Iteracion: ["+y+"] Enviando Cadena: " + paquete1[x]+"---"+i++);
                  sleep(500);
                }
              }

            }catch (InterruptedException ex) {
                Logger.getLogger(EnviaDatosPuerto.class.getName()).log(Level.SEVERE, null, ex);
              } 
            catch (IOException e) {
            }
        }
      }
    }

  }
}
